package com.happyclub.paul.service.impl;

import com.happyclub.paul.dao.PersonalPhotoAlbumRepostitory;
import com.happyclub.paul.dao.PhotoAlbumRepostitory;
import com.happyclub.paul.service.*;
import com.happyclub.pojo.TbAlbumPhotoMap;
import com.happyclub.pojo.TbPhotoAlbum;
import com.happyclub.pojo.TbPhotoLibrary;
import com.happyclub.pojo.TbUser;
import com.happyclub.utils.JsonResult;
import com.happyclub.utils.Uuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 魏泉琦
 * @Date: 2019/4/3 15:45
 * @Description:  相册实现类
 */
@Service
public class PhotoAlbumServiceImpl implements PhotoAlbumService {

    @Autowired
    private PhotoAlbumRepostitory photoAlbumRepostitory;

    @Autowired
    private PersonalPhotoAlbumRepostitory personalPhotoAlbumRepostitory;

    @Autowired
    private PersonalPhotoAlbumService personalPhotoAlbumService;


    @Autowired
    private IAlbumPhotoMapService albumPhotoMapService;         //相册相片中间表

    @Autowired
    private IPhotoLibraryService photoLibraryService;           //相片表

    @Autowired
    private IFilePathService filePathService;                   //文件上传

    @Autowired
    private IFastdfsService fastdfsService;                     //fastdfs

    /**
     *@Description: 保存用户创建的相册
     *@Author: 魏泉琦
     *@date: 2019/4/3
     */
    @Transactional
    @Override
    public JsonResult saveUserPhotoAlbumMsg(String userId, String photoAlbumName) {
        JsonResult jsonResult = new JsonResult();
        String photoAlbumId= Uuid.getUUID();       //创建相册编号uuid
        try{
            Integer cnt =  photoAlbumRepostitory.saveUserPhotoAlbumMsg(photoAlbumId,photoAlbumName);
            if(cnt>0){          //相册创建成功，那么要更新用户相册表
                //将用户编号和相册编号信息保存到用户相册表
                Integer count = personalPhotoAlbumService.savePersonalPhotoAlbumMsg(userId, photoAlbumId);
                if(count>0){    //用户相册信息保存成功
                    jsonResult.setMsg("相册创建成功");
                    jsonResult.setErrorCode("OK");
                    return jsonResult;
                }
            }
            //相册创建失败，提示用户
            jsonResult.setErrorCode("NO");
            jsonResult.setMsg("相册创建失败!!");

        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
            jsonResult.setErrorCode("error");
            e.printStackTrace();
        }
        return jsonResult;
    }

    /**
     *@Description: 通过中间表的相册编号查询主表中的相册信息
     *@Author: 魏泉琦
     *@date: 2019/4/6
     */
    @Override
    public TbPhotoAlbum queryPhotoAlbumsById(String photoAlbumId) {
        TbPhotoAlbum photoAlbums = photoAlbumRepostitory.queryPhotoAlbumsById(photoAlbumId);
        return photoAlbums;
    }

    /**
     *@Description: 通过相册编号删除对应相册
     *@Author: 魏泉琦
     *@date: 2019/4/6
     */
    @Transactional
    @Override
    public JsonResult deletePhotoAlbumById(String albumId) {
        JsonResult jsonResult = new JsonResult();
        try{
            //先删除用户相册表对应相册
            Integer cnt = personalPhotoAlbumRepostitory.deletePersonalPhotoAlbumByPhotoAlbumId(albumId);
            if(cnt<=0){   //用户相册表删除失败
               jsonResult.setMsg("相册删除失败");
               jsonResult.setErrorCode("500");
               return jsonResult;
            }

            //删除相册相片中间表
            Integer cnt1 = albumPhotoMapService.deletePhotoAlbumMapByAlbumId(albumId);
            if(cnt<=0){          //相册相片中间表删除失败
                jsonResult.setMsg("相册相册相片删除失败");
                jsonResult.setErrorCode("500");
                return jsonResult;
            }

            Integer integer = photoAlbumRepostitory.deletePhotoAlbumById(albumId);                   //删除相册主表
            if(integer<=0){
                jsonResult.setMsg("相册删除失败");
                jsonResult.setErrorCode("500");
                return jsonResult;
            }
        }catch (Exception e){
            jsonResult.setErrorCode("error");
            jsonResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        jsonResult.setErrorCode("OK");
        jsonResult.setMsg("相册删除成功");
        return jsonResult;
    }


    /**
     *@Description: 通过相册编号查询所拥有的相片
     *@Author: 魏泉琦
     *@date: 2019/4/7
     */
    @Override
    public JsonResult queryPhotoAlbumEveryPhoto(String albumId) {
        JsonResult jsonResult = new JsonResult();
        try{
            // 先根据相册编号，中间表，查询出相片信息
            List<TbAlbumPhotoMap> albumPhotoMaps = albumPhotoMapService.queryAlbumPhotosByaAlbumId(albumId);
            if(albumPhotoMaps==null){              //该相册没有相片
                jsonResult.setMsg("你的相册还没有相片哦");
                jsonResult.setErrorCode("NO");
                return jsonResult;
            }

            //相册拥有相片
            TbPhotoAlbum photoAlbum = new TbPhotoAlbum();           //相册对象
            List<TbPhotoLibrary> photoLibraries = new ArrayList<>();
            for(TbAlbumPhotoMap albumPhotoMap:albumPhotoMaps){
                TbPhotoLibrary photoLibrary = photoLibraryService.queryPhotoLibraryByPhotoId(albumPhotoMap.getPhotoId());
                photoLibraries.add(photoLibrary);
            }
            photoAlbum.setId(albumId);
            photoAlbum.setPhotoLibraries(photoLibraries);


            jsonResult.setErrorCode("OK");
            jsonResult.setResult(photoAlbum);
        }catch (Exception e){
            jsonResult.setErrorCode("error");
            jsonResult.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return jsonResult;
    }





    /**
     *@Description: 相册 上传相片
     *@Author: 魏泉琦
     *@date: 2019/4/8
     */
    @Override
    public JsonResult uploadPhotoAlbumFile(String albumId, MultipartFile[] multipartFiles, String  userId,String activityId) {
        JsonResult jsonResult =null;
        for(MultipartFile multipartFile:multipartFiles) {
            jsonResult = filePathService.upload(multipartFile);
            String errorCode = jsonResult.getErrorCode();
            System.out.println("测试文件是否上传到本地：" + errorCode);
            if ("error".equals(errorCode)) {             //上传失败
                return jsonResult;
            }
            if ("OK".equals(errorCode)) {               //上传成功，提交到fastdfs,返回id
                String filePath = (String) jsonResult.getResult();
                try {
                    String fastdfsId = fastdfsService.uploadFile(filePath);     //获取到标识
                    System.out.println(fastdfsId);

                    String photoUrl = "http://47.107.81.116/" + fastdfsId;
                    //将上传的相片保存到相片表，为 未审核状态 0
                    Integer cnt = photoLibraryService.createPhotoLibrary(Uuid.getUUID(), photoUrl, userId, albumId, 0,activityId);

                } catch (Exception e) {
                    jsonResult.setMsg("出现异常");
                    jsonResult.setErrorCode("500");
                    e.printStackTrace();
                }
            }
        }
        return  jsonResult;
    }

    /**
     *@Description: 查询要修改的相册信息，回显
     *@Author: 魏泉琦
     *@date: 2019/4/9
     */
    @Override
    public JsonResult queryEditAlbumNameByAlbumId(String albumId) {
        JsonResult jsonResult = new JsonResult();
        TbPhotoAlbum photoAlbum = photoAlbumRepostitory.queryEditAlbumNameByAlbumId(albumId);
        jsonResult.setResult(photoAlbum);
        return jsonResult;
    }

    /**
     *@Description: 保存修改的相册信息
     *@Author: 魏泉琦
     *@date: 2019/4/9
     */
    @Transactional
    @Override
    public JsonResult saveEditAlbumName(String albumId, String albumName) {
        JsonResult jsonResult = new JsonResult();
        Integer cnt = photoAlbumRepostitory.saveEditAlbumName(albumId, albumName);
        if(cnt<=0){    //修改失败
            jsonResult.setMsg("网络出现波动");
            jsonResult.setErrorCode("500");
            return jsonResult;
        }
        return jsonResult;
    }
}
